package org.jadestudio.home.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jadestudio.home.service.ArticleService;
import org.jadestudio.home.service.LeaderboardService;

public class ArticleListAction extends Action {
	private ArticleService articleService;
	private LeaderboardService leaderboardService;
	
	public ArticleService getArticleService() {
		return articleService;
	}
	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}
	
	public LeaderboardService getLeaderboardService() {
		return leaderboardService;
	}
	public void setLeaderboardService(LeaderboardService leaderboardService) {
		this.leaderboardService = leaderboardService;
	}
	
	/**
	 * 加载数据
	 */
	@SuppressWarnings("unchecked")
	public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		
		String category = request.getParameter("category");
		if (category==null) category="";
		String ps = request.getParameter("p");
		int p = 1;
		if (ps!=null && !ps.equals("")) p = Integer.parseInt(ps);		
		
		// 加载文章类型列表
		List<java.util.Map> articleTypeList = articleService.getArticleTypeList();
		request.setAttribute("articleTypeList", articleTypeList);
		
		// 加载筛选游戏列表
		List<java.util.Map> articleList = articleService.getArticleList(category, p, 20);
		request.setAttribute("articleList", articleList);
		
		// 加载筛选页总数
		int pageCount = articleService.getArticlePageCount(category, 20);
		request.setAttribute("pageCount", pageCount);
		
		// 分类文章TOP10
		List<java.util.Map> articleTop10 = leaderboardService.getArticleTop10ByType(category);
		request.setAttribute("articleTop10", articleTop10);
		
		// 游戏/文章总数
		java.util.Map count = leaderboardService.getGameArticleCount();
		request.setAttribute("count", count);
		
		return mapping.findForward("success");
	}
	
	
}
